package com.easybbs.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.easybbs.controller.base.ABaseController;
import com.easybbs.entity.dto.SysSettingDto;
import com.easybbs.entity.enums.UserRoleEnum;
import com.easybbs.entity.po.UserInfo;
import com.easybbs.entity.vo.ResponseVO;
import com.easybbs.entity.vo.request.AccountLoginReqVO;
import com.easybbs.entity.vo.request.AccountRegisterReqVO;
import com.easybbs.entity.vo.request.AccountResetPwdReqVO;
import com.easybbs.entity.vo.request.AccountSendEmailCodeReqVO;
import com.easybbs.entity.vo.response.AccountCheckCodeRspVO;
import com.easybbs.entity.vo.response.AccountGetUserInfoRspVO;
import com.easybbs.entity.vo.response.AccountLoginRspVO;
import com.easybbs.service.AccountService;
import com.easybbs.utils.SysCacheUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.awt.*;
import java.io.IOException;

/**
 * @Author: yudaxian
 * @Description: 账户相关接口
 * @DateTime: 2024/10/18 10:08
 **/
@Api(tags = "账户相关接口")
@RestController
public class AccountController extends ABaseController {

    @Resource
    private AccountService accountService;

    /**
     * 获取验证码
     * @param type
     * @return
     * @throws IOException
     * @throws FontFormatException
     */
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @PostMapping("/checkCode")
    public ResponseVO checkCode(@RequestParam("type") @Validated
                                    @NotNull(message = "验证码类型不能为空") Integer type) throws IOException, FontFormatException {
        /**
         * 验证码类型： 0-登录，注册，1：邮箱验证码
         */
        AccountCheckCodeRspVO vo = accountService.checkCode(type);

        return getSuccessResponseVO(vo);
    }

    /**
     * 发送邮箱验证码
     * @param reqVO
     * @return
     */
    @ApiOperation(value = "发送邮箱验证码", notes = "发送邮箱验证码")
    @PostMapping("/sendEmailCode")
    public ResponseVO sendEmailCode(@RequestBody @Validated AccountSendEmailCodeReqVO reqVO) {

        accountService.sendEmailCode(reqVO);

        return getSuccessResponseVO(null);
    }

    /**
     * 注册
     * @param reqVO
     * @return
     */
    @ApiOperation(value = "注册", notes = "注册")
    @PostMapping("/register")
    public ResponseVO register(@RequestBody @Validated AccountRegisterReqVO reqVO) {

        accountService.register(reqVO);

        return getSuccessResponseVO(null);

    }

    /**
     * 获取系统设置
     * @return
     */
    @ApiOperation(value = "获取系统设置", notes = "获取系统设置")
    @SaCheckLogin
    @PostMapping("/getSysSetting")
    public ResponseVO getSysSetting() {
        SysSettingDto sysSetting = SysCacheUtils.getSysSetting();

        return getSuccessResponseVO(sysSetting);
    }

    /**
     * 登录
     * @param reqVO
     * @return
     */
    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping("/login")
    public ResponseVO login(@RequestBody @Validated AccountLoginReqVO reqVO) {

        AccountLoginRspVO rspVO = accountService.login(reqVO);

        return getSuccessResponseVO(rspVO);

    }

    /**
     * 获取用户信息
     * @return
     */
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @SaCheckLogin
    @PostMapping("/getUserInfo")
    public ResponseVO getUserInfo() {

        UserInfo userInfo = accountService.getOnlyLoginUserInfo();
        AccountGetUserInfoRspVO rspVO = AccountGetUserInfoRspVO.builder()
                .nickName(userInfo.getNickName())
                .userId(userInfo.getUserId())
                .role(userInfo.getRole())
                .province(userInfo.getLastLoginIpAddress())
                .isAdmin(userInfo.getRole().equals(UserRoleEnum.ADMIN.getRoleName()))
                .build();
        return getSuccessResponseVO(rspVO);
    }

    /**
     * 退出登录
     * @return
     */
    @ApiOperation(value = "退出登录", notes = "退出登录")
    @SaCheckLogin
    @PostMapping("/logout")
    public ResponseVO logout() {
        StpUtil.logout();
        return getSuccessResponseVO(null);
    }

    /**
     * 重置密码
     * @return
     */
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @PostMapping("/resetPwd")
    public ResponseVO resetPwd(@RequestBody @Validated AccountResetPwdReqVO reqVO) {

        accountService.resetPwd(reqVO);

        return getSuccessResponseVO(null);
    }

}
